<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_khmerhr_admin extends CI_Model{
	function getUser($limit, $offest){
		$this->db->select("tbl_users.id,tbl_groups.name,tbl_groups.id as id_type,tbl_users.username,tbl_users.email,tbl_users.company,tbl_users.phone,tbl_users.contact_person,tbl_users.first_name,tbl_users.last_name");
		$this->db->from("tbl_users");
		$this->db->join("tbl_users_groups","tbl_users_groups.user_id = tbl_users.id");
		$this->db->join("tbl_groups","tbl_groups.id = tbl_users_groups.group_id");
		$this->db->where("tbl_groups.name !=","admin");
		$this->db->limit($limit,$offest);
		return $this->db->get();
	}
	/* ..........Delete.......... */

	function deleteEmployee($id){
		$this->db->where("tbl_users.id",$id);
		$this->db->delete("tbl_users");
		$this->db->where("tbl_users_groups.user_id",$id);
		$result = $this->db->delete("tbl_users_groups");
		return $result;
	}
	function deleteCategory($idCate){
		$this->db->where("tbl_jobtype.jobTypeID",$idCate);
		return $this->db->delete("tbl_jobtype");
	}
	function deleteLocation($id){
		$this->db->where("tbl_location.locationID",$id);
		return $this->db->delete("tbl_location");
	}

	/* .................... */
	function getDetailUser($id){
		$this->db->select("tbl_users.id,tbl_users.first_name,tbl_users.last_name,tbl_groups.name,tbl_users.username,tbl_users.email,tbl_users.company,tbl_users.phone,tbl_users.contact_person");
		$this->db->where("tbl_users.id",$id);
		$this->db->from("tbl_users");
		$this->db->join("tbl_users_groups","tbl_users_groups.user_id = tbl_users.id");
		$this->db->join("tbl_groups","tbl_groups.id = tbl_users_groups.group_id");
		$result = $this->db->get();
		return $result;
	}
	function updateUserProfile($firstName,$company,$contactPerson,$phone,$lastName,$type,$id){
		$arr = array(
			"first_name"=>$firstName,
			"company"=>$company,
			"contact_person"=>$contactPerson,
			"phone"=>$phone,
			"last_name"=>$lastName
		);
		$this->db->where("id",$id);
		$this->db->update("tbl_users",$arr);
		$this->db->set("group_id",$type);
		$this->db->where("user_id",$id);
		$this->db->update("tbl_users_groups");

	}
	function getUserType(){
		$this->db->select("*");
		$this->db->from("tbl_groups");
		return $this->db->get();
	}

	


	function getDetailLocation($id){
		$this->db->where("locationID",$id);
		$this->db->limit(1);
		return $this->db->get("tbl_location");
	}

	function getDetailCategory($id){
		$this->db->where("jobTypeID",$id);
		$this->db->limit(1);
		return $this->db->get("tbl_jobtype");
	}

	function addUsers($firstName,$lastName,$type,$email,$phone,$companyName,$contactPerson,$address){
		
		$arr = array(
			"first_name"=>$firstName,
			"last_name"=>$lastName,
			"email"=>$email,
			"phone"=>$phone,
			"contact_person"=>$contactPerson,
			"company"=>$companyName,
			"username"=>$firstName.'.'.$lastName,
			"address"=>$address
		);
		$data = $this->db->insert("tbl_users",$arr);
		$id = $this->db->insert_id();
		$this->db->set("group_id",$type);
		$this->db->set("user_id",$id);
		$data1 = $this->db->insert("tbl_users_groups");
		if($data && $data1){
			return true;
		}else{
			return false;
		}
	}
	function getCategory($limit, $offest){
		$this->db->order_by("jobTypeName DESC");
		$this->db->limit($limit,$offest);
		return $this->db->get("tbl_jobtype");
	}
	function getLocation($limit, $offest){
		$this->db->order_by("locationName DESC");
		$this->db->limit($limit,$offest);
		return $this->db->get("tbl_location");
	}
	function addCategory($name,$description){
		$this->db->set("jobTypeName",$name);
		$this->db->set("jobTypeDescription",$description);
		$this->db->insert("tbl_jobtype");
	}
	function addLocation($name,$description){
		$this->db->set("locationName",$name);
		$this->db->set("description",$description);
		$this->db->insert("tbl_location");
	}
	function updatepostjob($id,$comName,$jobType,$jobTitle,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee){
		$arrPostJob = array(
			"com_id"=>$comName,
			"job_post_title"=>$jobTitle,
			"job_time_id"=>$jobType,
			"salary"=>$salary,
			"hiring_num"=>$hiring,
			"post_date"=>$date_post,
			"close_date"=>$date_close,
			"how_to_apply"=>$howToApply,
			"requirement"=>$requirement,
			"post_responsible"=>$duty,
			"employeeId"=>$employee
		);
		$this->db->where("job_post_id",$id);
		$this->db->update("tbl_job_post",$arrPostJob);
	}
	function addPostJobExit($comName,$jobType,$jobTitle,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee){

		$arrPostJob = array(
			"com_id"=>$comName,
			"job_post_title"=>$jobTitle,
			"job_time_id"=>$jobType,
			"salary"=>$salary,
			"hiring_num"=>$hiring,
			"post_date"=>$date_post,
			"close_date"=>$date_close,
			"how_to_apply"=>$howToApply,
			"requirement"=>$requirement,
			"post_responsible"=>$duty,
			"employeeId"=>$employee
		);
		$this->db->insert("tbl_job_post",$arrPostJob);
		$post_id = $this->db->insert_id();
		$arr_pro = array();
		foreach ($provinces as $key => $value_province) {
			$arr_pro[] = array(
				"province_id"=>$value_province,
				"job_post_id"=>$post_id
			);
		}
		$this->db->insert_batch("tbl_post_province",$arr_pro);
		$arr_cate = array();
		foreach ($cate as $key => $value_cate) {
			$arr_cate[] = array(
				"category_id"=>$value_cate,
				"job_post_id"=>$post_id
			);
		}
		$this->db->insert_batch("tbl_post_cate",$arr_cate);
	}
	function addPostJob($comName,$jobType,$jobTitle,$companyDes,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee){
		
		$arrCom = array(
			"com_name"=>$comName,
			"description"=>$companyDes,
			"user_id"=>$this->session->userdata("userId")
		);
		$this->db->insert("tbl_company",$arrCom);
		$id = $this->db->insert_id();
		$arrPostJob = array(
			"com_id"=>$id,
			"job_post_title"=>$jobTitle,
			"job_time_id"=>$jobType,
			"salary"=>$salary,
			"hiring_num"=>$hiring,
			"post_date"=>$date_post,
			"close_date"=>$date_close,
			"how_to_apply"=>$howToApply,
			"requirement"=>$requirement,
			"post_responsible"=>$duty,
			"employeeId"=>$employee
		);
		$this->db->insert("tbl_job_post",$arrPostJob);
		$post_id = $this->db->insert_id();
		$arr_pro = array();
		foreach ($provinces as $key => $value_province) {
			$arr_pro[] = array(
				"province_id"=>$value_province,
				"job_post_id"=>$post_id
			);
		}
		$this->db->insert_batch("tbl_post_province",$arr_pro);
		$arr_cate = array();
		foreach ($cate as $key => $value_cate) {
			$arr_cate[] = array(
				"category_id"=>$value_cate,
				"job_post_id"=>$post_id
			);
		}
		$this->db->insert_batch("tbl_post_cate",$arr_cate);
	}
	function getCompanyExit($id){
		$this->db->where("user_id",$id);
		return $this->db->get("tbl_company");
	}
	function getCompanyId($id){
		$this->db->where("com_id",$id);
		return $this->db->get("tbl_company");
	}
	function getAdmin($username,$password){
		$this->db->select("*");
		$this->db->from("tbl_users");
		$this->db->join("tbl_users_groups","tbl_users_groups.user_id = tbl_users.id");
		$this->db->where("tbl_users.username",$username);
		$this->db->where("tbl_users.password",$password);
		$this->db->where("tbl_users_groups.group_id",1);
		$this->db->limit(1);
		$query = $this->db->get();
        if($query->num_rows() == 1) { 

            return $query->row(); 
        } else {
            return false; 
        }
	}


	/** Developer Thymeak**/

	function getViewJob($limit,$offest){
		$this->db->select("tbl_job_post.job_post_id,tbl_job_post.job_post_title,tbl_job_post.salary,tbl_job_post.post_date,tbl_job_post.close_date,tbl_company.com_name,tbl_job_time.job_time_name");
		$this->db->from("tbl_job_post");
		$this->db->join("tbl_company","tbl_job_post.com_id = tbl_company.com_id");
		$this->db->join("tbl_job_time","tbl_job_post.job_time_id = tbl_job_time.job_time_id");
		$this->db->limit($limit,$offest);
		return $this->db->get();
	}

	function deletejob($jobid){
		$this->db->where('job_post_id', $jobid);
		$this->db->delete('tbl_job_post');
	}

	function editLocation($id,$name,$des){
		$this->db->set("locationName",$name);
		$this->db->set("description",$des);
		$this->db->where("locationID",$id);
		$this->db->update("tbl_location");
	}

	function editCategory($id,$name,$des){
		$this->db->set("jobTypeName",$name);
		$this->db->set("jobTypeDescription",$des);
		$this->db->where("jobTypeID",$id);
		$this->db->update("tbl_jobtype");
	}
	function count_table($table){
		return $this->db->count_all_results($table);
	}
	/** end of Thymeak **/
}
